package com.ss.task.rest;

import com.ss.task.dto.TaskInfo;
import com.ss.task.dto.TaskUsers;
import com.ss.task.model.LeaderEntity;
import com.ss.task.model.TaskEntity;
import com.ss.task.model.TaskUserEntity;
import com.ss.task.model.UserEntity;
import com.ss.task.service.TaskService;
import com.ss.task.service.UserService;
import com.ss.task.util.BaseController;
import com.ss.task.util.Util;
import com.ss.webutil.struct.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import testPush02.JPush;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by logicRabbit on 9/22/15.
 */
@RestController
@RequestMapping("/rest/app/")
public class AppController extends BaseController{


    private static final String appKey ="caa91ebb108b6c38186a183c";
    private static final String masterSecret = "1fe444716a724e6e5a591909";
    @Autowired
    UserService userService;

    @Autowired
    TaskService taskService;


    @RequestMapping("login")
    public String login(String username, String password, HttpSession session){
        //System.out.println("djfdjfjs");
        ResultModel<UserEntity> user = userService.login(username, password);
        if(user.getStatus() != 0)
            return user.getStatus().toString();
        else {
            UserEntity userData = user.getData();
            session.setAttribute("user", user);
            List<LeaderEntity> leaders = userService.getLeaderByUser(userData.getId()).getData();
            session.setAttribute("isLeader", leaders.size() != 0);
            session.setAttribute("taskNum", taskService.getCountOfAllPubTasksByMember(userData.getId()).getData());
            return user.getStatus().toString();
        }

    }

    @RequestMapping("task/allTaskList")
    public ResultModel<List<TaskEntity>> allTaskList(){
        return taskService.getAllPubTasks(0, Integer.MAX_VALUE);
    }

    @RequestMapping("task/pubTaskListByMember") //执行的任务
    public ResultModel<List<TaskUserEntity>> pubTaskListByMember(HttpSession session){
        UserEntity user = (UserEntity)session.getAttribute("user"); // session 都没测试！！！
        return taskService.getAllPubTasksByMember(user.getId(), 0, Integer.MAX_VALUE);
        //return taskService.getAllPubTasksByMember(8, 0, Integer.MAX_VALUE);
    }

    @RequestMapping("task/pubTaskListByUser")//发布的任务
    public ResultModel<List<TaskEntity>> pubTaskListByUser(HttpSession session){
        UserEntity user = getUser(session);
        return taskService.getAllPubTasksByPubUser(user.getId(), 0, Integer.MAX_VALUE);
        //return taskService.getAllPubTasksByPubUser(32, 0, Integer.MAX_VALUE);
    }

    @RequestMapping("task/taskInfo")//任务详情
    public ResultModel<TaskInfo> getTaskInfo(Integer tid) {
        return taskService.getTask(tid);
        //return taskService.getTask(26); //怪怪的 参数婷姐原来版本写的有点不对！这里没有放上去
    }


    @RequestMapping("users/follower")//查询下属
    public ResultModel<List<UserEntity>> getFollower(HttpSession session) {
        UserEntity user = getUser(session);
        return userService.getUsersByLeader(user.getId());
        //return userService.getUsersByLeader(32);
    }

    @RequestMapping("task/addTask")
    public ResultModel<TaskEntity> addTask(TaskEntity task, String sd, String ed, TaskUsers tu, HttpSession session) {

        UserEntity user = getUser(session);

        task.setStartDate(Util.parseDate(sd));
        task.setEndDate(Util.parseDate(ed));
        List<TaskUserEntity> taskUserEntityList = tu.getTaskusers();
        for(int i=0;i<taskUserEntityList.size();i++){
            JPush.testSendPush("caa91ebb108b6c38186a183c", "1fe444716a724e6e5a591909", task, taskUserEntityList.get(i).getUser().getId().toString(), taskUserEntityList.get(i).getDetail());
        }


        return taskService.addTask(user.getId(), task, tu.getTaskusers());
    }
}




